package stu.xjtu.physical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import stu.xjtu.physical.common.constant.JwtClaimsConstant;
import stu.xjtu.physical.common.properties.JwtProperties;
import stu.xjtu.physical.common.utls.JwtHelper;
import stu.xjtu.physical.common.utls.JwtUtil;
import stu.xjtu.physical.mapper.FamliyMapper;
import stu.xjtu.physical.model.domain.Famliy;
import stu.xjtu.physical.model.domain.User;
import stu.xjtu.physical.mapper.UserMapper;
import stu.xjtu.physical.model.dto.LoginDto;
import stu.xjtu.physical.model.vo.FamliesVo;
import stu.xjtu.physical.model.vo.LoginVo;
import stu.xjtu.physical.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lsong
 * @since 2024-03-31
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Resource
    private FamliyMapper famliyMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @Override
    public LoginVo login(LoginDto user) {
        LoginVo res = new LoginVo();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",user.getPhone());
//                .eq("identify_code",user.getCode());
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1 == null) {
            User user2 = new User();
            user2.setPhone(user.getPhone());
//            user2.setIdentifyCode("1111");
            userMapper.insert(user2);
            // famliy 表里面默认的返回一个
            Famliy admin = new Famliy();
            admin.setName("管理员");
            admin.setAvator("../../static/images/user.jpg");
            admin.setUId(user2.getId());
            admin.setInfo(1);
            famliyMapper.insert(admin);
            res.setUserId(user2.getId());
            res.setCurrFamliy(admin.getId());
            res.setAvator(admin.getAvator());
        }else {
            res.setUserId(user1.getId());
            QueryWrapper<Famliy> query = new QueryWrapper<>();
            query.eq("u_id",user1.getId()).eq("info",1);
            Famliy famliy = famliyMapper.selectOne(query);
            res.setName(famliy.getName());
            res.setAvator(famliy.getAvator());
            res.setCurrFamliy(famliy.getId());
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, res.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims);
        res.setToken(token);
        return res;
    }

    @Override
    public FamliesVo getFamlies(String token) {
        Long userId = jwtHelper.getFrontUserId(token);
        QueryWrapper<Famliy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",userId);
        List<Famliy> famliys = famliyMapper.selectList(queryWrapper);
        FamliesVo res = new FamliesVo();
        res.famlies = famliys;
        return res;
    }
}
